<% for (var chunk in htmlWebpackPlugin.files.chunks) { %>
  <% if (htmlWebpackPlugin.options.isDevServer === true) { %>
    <% var src = htmlWebpackPlugin.files.chunks[chunk].entry %>
  <% } else { %>
    <% var basename = htmlWebpackPlugin.files.chunks[chunk].entry.split('/').reverse()[0]; %>
    <% var src = `{{ '${basename}' | asset_url }}` %>
  <% } %>

  <% if (typeof htmlWebpackPlugin.options.liquidLayouts[chunk] !== 'undefined') { %>
    {%- if layout == '<%= chunk.split('.')[1] %>' -%}
      <script type="text/javascript" src="<%= src %>" defer></script>
    {%- else -%}
      <link rel="prefetch" href="<%= src %>" as="script">
    {%- endif -%}
    
  <% } else if (chunk.split('@').length > 1) { %>
    <% var pages = chunk.split('@') %>
    <% var conditions = [] %>

    <% pages.forEach(function(page){ %>
      <% var pageTemplate = htmlWebpackPlugin.options.liquidTemplates[page] %>
      <% if (typeof pageTemplate !== 'undefined') { %>
        <% if (pageTemplate.includes('customers\\') || pageTemplate.includes('customers/')) { %>
          <% conditions.push("template == 'customers/" + page.split('.').slice(1).join('.') + "'") %>
        <% } else { %>
          <% conditions.push("template == '" + page.split('.').slice(1).join('.') + "'") %>
        <% } %>
      <% } else if (typeof htmlWebpackPlugin.options.liquidLayouts[page] !== 'undefined') { %>
        <% conditions.push("layout == '" + page.split('.')[1] + "'") %>
      <% } %>
    <% }); %>

    {%- if <%= conditions.join(' or ') %> -%}
      <script type="text/javascript" src="<%= src %>" defer></script>
    {%- else -%}
      <link rel="prefetch" href="<%= src %>" as="script">
    {%- endif -%}

  <% } else if (typeof htmlWebpackPlugin.options.liquidTemplates[chunk] !== 'undefined') { %>
    <% var chunkTemplate = htmlWebpackPlugin.options.liquidTemplates[chunk] %>
    <% if (chunkTemplate.includes('customers\\') || chunkTemplate.includes('customers/')) { %>
      {%- if template == 'customers/<%= chunk.split('.').slice(1).join('.') %>' -%}
        <script type="text/javascript" src="<%= src %>" defer></script>
      {%- else -%}
        <link rel="prefetch" href="<%= src %>" as="script">
      {%- endif -%}
    <% } else { %>
      {%- if template == '<%= chunk.split('.').slice(1).join('.') %>' -%}
        <script type="text/javascript" src="<%= src %>" defer></script>
      {%- else -%}
        <link rel="prefetch" href="<%= src %>" as="script">
      {%- endif -%}
    <% } %>
  <% } else { %>
    <script type="text/javascript" src="<%= src %>" defer></script>
  <% } %>
<% } %>
